/**
 * 功能描述�?
 */

define(function (require, exports, module) {
	var aw = require("ajaxwrapper");
	var Dialog = require("dialog");
	var Editgrid = require("editgrid");
	
    module.exports = {
        checkData : function(params, widget){
        	var that = this;
        	widget.hide([".J-grid"]).show([".J-form", ".J-pergrid",".J-pereditgrid"]);
        	if(widget.get("type") == "NonMember"){
        		widget.get("subnav").show(["return"]).hide(["status","servicePoint", "startDate", "add"]);
        	}else{
        		widget.get("subnav").show(["return"]).hide(["status", "operateDate", "add", "search"]);
        	}
        	var data = params.nonMemberGroup;
        	var form = widget.get("form");
        	form.setData(data);
        	/*form.setValue("count", params.signUpRooms[0].count);
        	form.setValue("roomType",params.signUpRooms[0].roomType);*/
        	form.setDisabled(true);
        	if(!data.chargeDeposit){
        		form.setValue("chargeDeposit", "false");
        	}
        	var pereditgrid = widget.get("pereditgrid");
        	var pergrid = widget.get("pergrid");
        	//form.setValue("count", params.signUpRooms[0].count);
        	aw.ajax({
        		url : "api/nonmemberregister/query",
        		data : {
        			"nonMemberGroup" : params.nonMemberGroup.pkNonMemberGroup,
        			fetchProperties : "*,signUpRegister.contactName,signUpRegister.contactPhone," +
        					"signUpRegister.exitGroup," +
        					"signUpRegister.personalInfo.*," +
        					"signUpRegister.travelContract.*," +
        					"signUpRegister.travelContract.productRule.product.name," +
        					"signUpRegister.travelContract.member.personalInfo.*"
        		},
        		dataType : "json",
        		success : function(datas){
        			if(pereditgrid){//散客预约
                		pereditgrid.setData(params.signUpRooms);
                    	pereditgrid.get("model").allowEdit=false;
                    	pereditgrid.get("model").allowAdd=false;
                    	pereditgrid.get("model").allowFoot=false;
                	}else{
                		pergrid.setData(datas);
                	}
        			$(".J-grid-head-formel").hide();
        			$("th[data-name='operate']").hide();
        			//form.setValue("signUpNumber", that.formatSignUpNumber(datas));
        			//widget.get("pergrid").setData([]);
        			//widget.get("pergrid").setData(that.formatNotExitGroup(datas));
        			$(".J-grid-memberRegisterApplyGrid-delete").addClass("hidden")
        			if(widget.get("type") != "NonMember" || params.tourType && params.tourType.key != "IndivMember"){
        				$(".J-grid-columns-signUpRegister-travelContract-memberCertificates").addClass("hidden")
        			}
        		}
        	})
        },
        
        editData : function(params, widget){
        	var that = this;
        	widget.hide([".J-grid"]).show([".J-form", ".J-pereditgrid"]);
        	var form = widget.get("form");
        	var datas = {};
        	var pkNonMemberGroup = null;
        	if(widget.get("type") == "NonMember"){ //散客编辑
        		widget.get("subnav").show(["return", "save"]).hide(["status", "startDate", "add"]);
        		datas = params;
        		form.setValue("servicePoint", datas.nonMemberGroup.servicePoint);
        		form.setValue("checkInDate", moment(datas.nonMemberGroup.checkInDate).format("YYYY-MM-DD"));
            	form.setValue("checkOutDate", moment(datas.nonMemberGroup.checkOutDate).format("YYYY-MM-DD"));
            	form.setValue("contactName", datas.nonMemberGroup.contactName);
            	form.setValue("contactPhone", datas.nonMemberGroup.contactPhone);
            	form.setValue("chargeDeposit", datas.nonMemberGroup.chargeDeposit);
            	form.setValue("deposit", datas.nonMemberGroup.deposit);
            	form.setValue("perNumber", datas.nonMemberGroup.perNumber);
            	form.setValue("tourType", datas.nonMemberGroup.tourType.key);
            	form.setValue("isWaterElectric", datas.nonMemberGroup.isWaterElectric);
            	form.setValue("operatorName", datas.nonMemberGroup.operator.name);
            	form.setValue("operateDate", moment(datas.nonMemberGroup.operateDate).format("YYYY-MM-DD"));
            	form.setValue("description", datas.nonMemberGroup.description);
            	form.setValue("pkNonMemberGroup", datas.nonMemberGroup.pkNonMemberGroup);
            	form.setValue("version", datas.nonMemberGroup.version);
            	
            	aw.ajax({
            		url:"api/signuproom/querybyreserve",
            		data : {
            			"pkNonMemGroupReserve" : datas.pkNonMemGroupReserve,
            			fetchProperties:"roomType.name,count,payables,roomType.pkRoomType,pkSignUpRoom"
            		},
            		dataType : "json",
            		success : function(data){
            			widget.get("pereditgrid").setData(data);
            			
            		}
            	});
        	}else{	//入住登记体验
        		datas = params.nonMemberGroup;
        		form.setData(datas);
        		pkNonMemberGroup = datas.pkNonMemberGroup,
        		form.setValue("roomType", params.signUpRooms[0].roomType.pkRoomType);
            	form.setValue("count", params.signUpRooms[0].count);
            	form.setValue("operator", params.nonMemberGroup.operator.pkUser);
            	form.setValue("signUpType", params.nonMemberGroup.signUpType.key);
            	form.setValue("status", params.nonMemberGroup.status.key);
            	widget.show([".J-pergrid"]);
        		widget.get("subnav").show(["return", "save"]).hide(["status", "operateDate", "add","search"]);
        		aw.ajax({
            		url : "api/nonmemberregister/query",
            		data : {
            			"nonMemberGroup" : pkNonMemberGroup,
            			fetchProperties : "*,signUpRegister.contactName,signUpRegister.contactPhone," +
		            			"signUpRegister.exitGroup," +
		            			"signUpRegister.personalInfo.*," +
            					"signUpRegister.travelContract.*," +
            					"signUpRegister.travelContract.productRule.product.name," +
            					"signUpRegister.travelContract.member.personalInfo.*"
            		},
            		dataType : "json",
            		success : function(datass){
            			form.setValue("signUpNumber", datass.length);//that.formatSignUpNumber(datas));
//            			var perData=[];
//            			if(datass.length>0){
//            				for(var i=0;i<datass.length;i++){
//            					perData.push(datass[i].signUpRegister)
//            				}
//            			}
            			widget.get("pergrid").setData(datass);//that.formatNotExitGroup(datas));
            			if(widget.get("type") == "NonMember" && params.tourType.key == "IndivMember"){
            				$("th[data-name='signUpRegister.travelContract.memberCertificates']").show();
            			}else{
            				$("th[data-name='signUpRegister.travelContract.memberCertificates']").hide();
            				$(".J-grid-columns-signUpRegister-travelContract-memberCertificates").addClass("hidden");
            			}
            		}
            	})
        	}
        },
        
        deleteData : function(params, widget){
        	aw.del("api/nonmembergroup/"+params.nonMemberGroup.pkNonMemberGroup+"/deleteall",function(data){
        		widget.get("grid").refresh();
        	});
        },
        
        confirmData : function(params, widget){
        	Dialog.confirm({
    			title:"提示",
    			content:"是否已确认散客信息？",
    			confirm:function(){
    				aw.ajax({
    					url : "api/nonmembergroup/updatestatus",
    					data : {
    						"pkNonMemberGroup" : params.nonMemberGroup.pkNonMemberGroup,
    						"status" : "Confirm"
    					},
    					dataType : "json",
    					success : function(data){
    						widget.get("grid").refresh();
    					}
    				})
    			}
        	})
        },
        
        reserveData : function(params, widget){
        	
        	aw.ajax({
        		url : "api/nonmemgroupreserve/query",
        		data : {
        			"nonMemberGroup" : params.pkNonMemberGroup,
        			fetchProperties : "signUpRooms.*,signUpRooms.roomType.*"
        		},
        		dataType : "json",
        		success : function(datas){
        			if(datas.length != 0){
        				console.log(datas[0].signUpRooms);
        				var gridData = datas[0].signUpRooms;
        			}
        			widget.get("editgrid").setData(gridData);
        			widget.set("pkNonMemberGroup", params.pkNonMemberGroup);
        			widget.hide([".J-grid"]).show([".J-editgrid"]);
        			widget.get("subnav").show(["sure", "reserve", "cancel"]).hide(["status","servicePoint", "startDate", "add"]);
        		}
        	})
        	
        },
        
        formatSignUpNumber: function(data){
        	var signUpNumber = 0;
        	if(data){
        		for(var i = 0; i<data.length; i++){
        			if(!data[i].signUpRegister.exitGroup){
        				signUpNumber++
        			}
        		}
        	}
        	return signUpNumber;
        },
        formatNotExitGroup: function(data){
        	for(var i = 0; i<data.length; i++){
    			if(data[i].signUpRegister.exitGroup){
    				delete data[i];
    			}
    		};
    		return data;
        }
    };
});
